package com.warehouse_management.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.warehouse_management.config.PageModel;
import com.warehouse_management.purchase.model.Brand;
import com.warehouse_management.purchase.model.FirmCondition;
import com.warehouse_management.purchase.model.ProductCondition;
import com.warehouse_management.purchase.model.Type;
import com.warehouse_management.purchase.service.ProductService;
import com.warehouse_management.system.model.City;
import com.warehouse_management.system.model.Province;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @PACKAGE_NAME: com.warehouse_management.purchase.controller
 * @NAME: ProductController
 * @USER: ZhouYongCheng
 * @DATE: 2022/10/3
 * @TIME: 2:46
 * @Description: 商品管理控制层
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/queryAllProduct")
    @ResponseBody
    public String queryAllProduct(@RequestParam("pagenum") int pagenum, ProductCondition product, PageModel<ProductCondition> pageModel){
        pageModel.setPageNum(pagenum);
        PageModel<ProductCondition> pageModel1 = productService.PageProductPosition(product, pageModel);
        if (pageModel1.getData() != null){
            if (!pageModel1.getData().isEmpty()){
                System.out.println(pageModel1);
                return JSON.toJSONString(pageModel1);

            }
        }
        return null;
    }

    /**
     * 连小龙
     * 查询单个产品数据(模糊查询)
     * @param pagenum
     * @param type
     * @param brand
     * @param statu
     * @param pageModel
     * @return
     */
    @RequestMapping("/queryOneProduct")
    @ResponseBody
    public String queryOneProduct(@RequestParam("pagenum") int pagenum,@RequestParam("type") String type,@RequestParam("brand") String brand,@RequestParam("statu") String statu,@RequestParam("mode") String mode,PageModel<ProductCondition> pageModel){
        ProductCondition p = new ProductCondition();
        if (type.equals("请选择")){
            type = null;
        }
        if (brand.equals("请选择")){
            brand = null;
        }
        p.setTypeName(type);
        p.setBrandName(brand);
        p.setProductStatus(statu);
        p.setProductModel(mode);
        pageModel.setPageNum(pagenum);
        PageModel<ProductCondition> productListByCondition = productService.queryProductListByCondition(p, pageModel);
        System.out.println(productListByCondition);
        if (productListByCondition != null){
            System.out.println(productListByCondition);
            return JSON.toJSONString(productListByCondition);
        }
        return null;
    }

    @RequestMapping("/queryType")
    @ResponseBody
    public String queryProvince(){
        List<Type> types = productService.queryType();
        if (types != null){
            System.out.println("aa"+types);
            return JSON.toJSONString(types);
        }
        return null;
    }

    @RequestMapping("/queryBrand")
    @ResponseBody
    public String queryCity(@RequestParam("id") String id){
        List<Brand> brandList = productService.queryBrand(id);
        if (brandList != null){
            System.out.println(brandList);
            return JSON.toJSONString(brandList);
        }
        return null;
    }

    /**
     * 连小龙
     * 查看单个数据详情
     * @param id
     * @return
     */
    @RequestMapping("/queryProductDetails")
    @ResponseBody
    public String queryFirmDetails(@RequestParam("id") String id,PageModel<ProductCondition> pageModel){
        ProductCondition pc = new ProductCondition();
        pc.setProductId(id);
        PageModel<ProductCondition> productConditionPageModel = productService.queryProductListByCondition(pc, pageModel);
        if (productConditionPageModel != null){
            System.out.println(productConditionPageModel);
            return JSON.toJSONString(productConditionPageModel);
        }
        return null;
    }

    @RequestMapping("/addProduct")
    @ResponseBody
    public String addFirm(@RequestParam("pBrand") String pBrand,@RequestParam("pType") String pType,@RequestParam("pModel") String pModel,@RequestParam("pUnit") String pUnit,
                          @RequestParam("pStatus") String pStatus,@RequestParam("cPeople") String cPeople,PageModel<ProductCondition> pageModel){

        ProductCondition pc = new ProductCondition();
        if (pStatus.equals("可用")){
            pStatus = "1";
        }else if (pStatus.equals("不可用")){
            pStatus = "0";
        }
        pc.setProductModel(pModel);
        pc.setProductStatus(pStatus);
        pc.setBrandName(pBrand);
        pc.setTypeName(pType);
        pc.setProductUnit(pUnit);
        pc.setUname(cPeople);
        ProductCondition p = new ProductCondition();
        p.setBrandName(pBrand);
        PageModel<ProductCondition> pageModel1 = productService.queryProductListByCondition(p, pageModel);
        int j = 0;
        for (ProductCondition datum : pageModel1.getData()) {
            if (!datum.getProductModel().equals(pModel)){
                j++;
            }
        }
        if (j>0){
            return JSON.toJSONString("falied");
        }
        int i = productService.addProduct(pc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    @RequestMapping("/updateProduct")
    @ResponseBody
    public String updateFirm(@RequestParam("pId") String pId,@RequestParam("pBrand") String pBrand,@RequestParam("pType") String pType,@RequestParam("pModel") String pModel,@RequestParam("pUnit") String pUnit,
                             @RequestParam("pStatus") String pStatus,@RequestParam("pTypeId") String pTypeId,PageModel<ProductCondition> pageModel){

        ProductCondition pc = new ProductCondition();
        if (pStatus.equals("可用")){
            pStatus = "1";
        }else if (pStatus.equals("不可用")){
            pStatus = "0";
        }
        pc.setProductModel(pModel);
        pc.setProductId(pId);
        pc.setProductStatus(pStatus);
        pc.setBrandName(pBrand);
        pc.setTypeName(pType);
        pc.setProductUnit(pUnit);
        ProductCondition p = new ProductCondition();
        p.setTypeName(pTypeId);
        PageModel<ProductCondition> pageModel1 = productService.queryProductListByCondition(p, pageModel);
        int j = 0;
        for (ProductCondition datum : pageModel1.getData()) {
            if (!datum.getProductModel().equals(pModel)){
                j++;
            }
        }
        if (j>0){
            return JSON.toJSONString("falied");
        }
        int i = productService.updateProduct(pc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    @RequestMapping("/updateProductStatus")
    @ResponseBody
    public String updateProductStatus(@RequestParam("pId") String pId,@RequestParam("pStatus") String pStatus){


        ProductCondition pc = new ProductCondition();
        if (pStatus.equals("可用")){
            pStatus = "0";
        }else if (pStatus.equals("不可用")){
            pStatus = "1";
        }
        pc.setProductId(pId);
        pc.setProductStatus(pStatus);
        int i = productService.updateProductStatus(pc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }
}
